package com.itboye.lottery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.lottery.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wu on 2018/1/9.
 */

public class ChongZhiJineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<HashMap<String, Object>> classEntityArrayList = new ArrayList<>();

    public ChongZhiJineAdapter(ArrayList<HashMap<String, Object>> classEntityArrayList) {
        this.classEntityArrayList = classEntityArrayList;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(ChongZhiJineAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolderChongzhi(LayoutInflater.from(context).inflate(R.layout.item_chongzhi, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HashMap<String, Object> hash = classEntityArrayList.get(position);
        ((ViewHolderChongzhi) holder).item_tv_chongzhi_jine.setText(hash.get("text").toString());
        if ((boolean) hash.get("status")) {
            ((ViewHolderChongzhi) holder).item_tv_chongzhi_jine.setTextColor(context.getResources().getColor(R.color.white));
            ((ViewHolderChongzhi) holder).item_rl_chongzhi_jine.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.dotted_rounded_bg_red_border_red));
        } else {
            ((ViewHolderChongzhi) holder).item_tv_chongzhi_jine.setTextColor(context.getResources().getColor(R.color.main_color));
            ((ViewHolderChongzhi) holder).item_rl_chongzhi_jine.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.dotted_rounded_bg_white_border_red));
        }
//        GlideUtil.display(context, list.get(position).getImg_url(), holder.img);
//        holder.name.setText(list.get(position).getClassification_name());
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }

    public double getSelectMoney() {
        double money = 0;
        for (HashMap<String, Object> hash : classEntityArrayList) {
            if ((boolean) hash.get("status")) {
                money = Double.parseDouble(hash.get("value").toString());
                break;
            }
        }
        return money;
    }

    public class ViewHolderChongzhi extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView item_tv_chongzhi_jine;
        public RelativeLayout item_rl_chongzhi_jine;

        public ViewHolderChongzhi(View itemView) {
            super(itemView);
            item_rl_chongzhi_jine = itemView.findViewById(R.id.item_rl_chongzhi_jine);
            item_tv_chongzhi_jine = itemView.findViewById(R.id.item_tv_chongzhi_jine);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
